package main

import (
  "fmt"
  "math/rand"
  "time"
)

func sumArray(a [10]int/*这里只能接受元素个数为10的数组,怎么解决？ 切片*/) int {
  var sum int = 0
  //第一种遍历方式
  //for i := 0; i < len(a); i++ {
  //  sum = sum+a[i]
  //}

  //第二种遍历方式
  for _, val := range a {
    sum += val
  }

  return sum
}

func testArraySum(){
/*
//Seed uses the provided seed value to initialize the default Source to a
//deterministic state. If Seed is not called, the generator behaves as
//if seeded by Seed(1). Seed values that have the same remainder when
//divided by 2³¹-1 generate the same pseudo-random sequence.
//Seed, unlike the Rand.Seed method, is safe for concurrent use.
func Seed(seed int64) { globalRand.Seed(seed) }
*/
  //初始化随机数终止(如果不这样初始化的话 每次调用生成的随机数都是一样的(? what the fk))
  rand.Seed(time.Now().Unix())
  var b [10]int
  for i := 0; i < len(b); i++ {
    b[i] = rand.Intn(1000) // 0-999
    //println(b[i])
    //b[i] = rand.Int() //0-int的上线(21亿)
  }

  sum := sumArray(b)
  fmt.Printf("sum=%d\n",sum)
}

func main() {
  //print(sumArray([10]int{1,2,3,4,5,6,7,8,9,10}))

  testArraySum()
}
